package com.tanger.common.utils;

import cn.hutool.core.util.HexUtil;
import lombok.experimental.UtilityClass;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Description TODO
 * @Author Hchenbin
 * @Date 2023-12-25 14:18
 * @Version 1.0
 */
@UtilityClass
public class MD5Util {

    public static String encode(String src) {
        return encode(src, true);
    }

    public static String encode(String src, boolean lowerCase) {
        return encode(src, Charset.defaultCharset(), lowerCase);
    }

    public static String encode(String src, Charset charset, boolean lowerCase) {
        try {
            return HexUtil.encodeHexStr(MessageDigest.getInstance("MD5")
                    .digest(src.getBytes(charset)), lowerCase);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("md5 encode exception.", e);
        }
    }

}
